/*
 * @Author: chunlai
 * @Date: 2023-07-25 09:48:14
 * @LastEditTime: 2025-08-11 11:35:10
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \02.bldinsure\src\views\index\feecount\validate.js
 * @Description: 客户预试算信息录入校验规则
 */

import {
  idTypeRuleCheck,
  sexRuleCheck,
  birthDayRuleCheck,
} from "@/translate/validate/fileds.js";
import { IdentityCodeValid, emptyFieldCheck } from "@/methods/userinfos.js";
import store from "@/store/index";
import constantRules from "@/methods/rules.js";
import { getCacheValue } from "@/methods/cache.js";
import { createResult } from "@/translate/validate/customer";

const { regexMap } = constantRules;
/**
 * 校验客户信息
 * @param {*} userModel 字段模板
 * @param {*} infoData
 * @param {*} utype
 * @returns
 */
// const storeInsert = store.state.insert;

export const validateSimpleInfo = (userModel, infoData, utype = "") => {
  // console.log("validateSimpleInfo", userModel)
  let obj = {
    msg: "",
    status: true,
    target: "",
  };

  // 姓名校验
  if (userModel.name !== undefined) {
    obj = nameCheckRule(infoData.name);
    if (!obj.status) return obj;
  }
  //证件类型校验
  if (userModel.idType !== undefined) {
    obj = idTypeRuleCheck(infoData);
    if (!obj.status) return obj;
  }
  //证件号码校验
  if (userModel.idNum !== undefined) {
    obj = idNumCheck(infoData);
    if (!obj.status) return obj;
  }
  // 性别校验
  if (userModel.sex !== undefined) {
    obj = sexRuleCheck(infoData, utype);
    if (!obj.status) return obj;
  }
  // 出生日期
  if (userModel.birthDate !== undefined) {
    obj = birthDayRuleCheck(infoData, utype);
    if (!obj.status) return obj;
  }
  /**仅限被保人校验*/
  if (utype.indexOf("被保人") !== -1) {
    //社保
    if (userModel.socialInsuFlag !== undefined) {
      obj = emptyFieldCheck(infoData, "socialInsuFlag", "有无社保");
      if (!obj.status) return obj;
    }
    if (userModel.occupationCode !== undefined) {
      const { occupationCode, occupationCodeName } = infoData;
      if (!occupationCode || !occupationCodeName) {
        return createResult(false, `请选择职业`, "occupationCode");
      }
    }
    const noticeMode = getCacheValue("page", "noticeMode");
    if (userModel.healthyStatus !== undefined && noticeMode == "1") {
      if (infoData.healthyStatus == "") {
        return createResult(false, `请确认健康告知`, "healthyStatus");
      }
    }
  }

  return {
    ...obj,
  };
};

/**
 * 姓名校验
 * @param {*} nameValue
 * @param {*} typeVal
 * @returns
 */

export const nameCheckRule = (nameValue) => {
  // 预编译正则表达式
  const maxNum = store.state.page.nameMaxLength || 20;
  // 空值检查
  if (!nameValue || nameValue.trim() === "") {
    return createResult(false, `姓名不能为空`);
  }

  if (nameValue.length < 2) {
    return createResult(false, `姓名少于两个汉字`);
  }
  if (nameValue.length > maxNum) {
    return createResult(false, `姓名长度不能超过${maxNum}个汉字`);
  }
  if (!regexMap.CHINA.test(nameValue)) {
    return createResult(false, `姓名输入有误`);
  }

  return createResult(true);
};

/**
 * 证件号码校验
 * @param {*} userInfo
 * @param {*} userType
 * @returns
 */

export const idNumCheck = (userInfo) => {
  if (!userInfo || !userInfo.idNum || userInfo.idNum === "") {
    return createResult(false, `请输入证件号码`, "idNum");
  }
  // console.log("userInfo", userInfo.idType);
  try {
    switch (userInfo.idType) {
      case "0":
      case "3":
        if (!IdentityCodeValid(userInfo.idNum)) {
          return createResult(false, `身份证校验不通过`, "idNum");
        }
        break;
      case "11":
        const bronResult = validateWithRegex(
          userInfo.idNum,
          regexMap.bronReg,
          `证件号码不正确`
        );
        if (bronResult) return bronResult;
        break;
      default:
        break;
    }
  } catch (error) {
    // 捕获异常并返回错误结果
    console.warn("验证失败：", error);
  }
  return createResult(true);
};
// 定义通用验证函数
const validateWithRegex = (idNum, regex, errorMessage) => {
  if (!regex.test(idNum)) {
    return createResult(false, errorMessage, "idNum");
  }
  return null; // 验证通过返回 null
};
